#################################################################################### 
####################################################################################
####################################################################################
# Appendix G: Tables G.20 - G.23, M.53-M.56
####################################################################################
####################################################################################
library(here)
library(kableExtra)
library(lmtest)
library(multiwayvcov)
####################################################################################
####################################################################################
## Read in survey data (from: 8_2020_analysis.R)
dat = readRDS(here("data/dat_unrestricted.rds"))
datb = readRDS(here("data/dat_restricted.rds"))

# Implement SWE of ATE: Demean all covariates, fully interact
cols = c("sex","age","education_level","Arua","Bushenyi","Ibanda","Jinja","Mbale","Mpigi","Pallisa","Mbarara","Shema",
         "village_distance_HF","village_distance_HOSP","village_distance_road","village_distance_electricity", "share2006")
dat[, paste0("c_", cols)] = lapply(dat[, cols], function(x) (x - mean(x))  )
covariates = c("treatment*c_village_distance_HF + treatment*c_village_distance_HOSP + treatment*c_village_distance_road + treatment*c_village_distance_electricity + 
                treatment*c_sex + c_age*treatment + c_education_level*treatment + treatment*c_share2006 + 
                c_Arua*treatment + c_Bushenyi*treatment + c_Ibanda*treatment + c_Jinja*treatment + c_Mbale*treatment + c_Mpigi*treatment + c_Pallisa*treatment + c_Mbarara*treatment",
               "c_Arua*treatment + c_Bushenyi*treatment + c_Ibanda*treatment + c_Jinja*treatment + c_Mbale*treatment + c_Mpigi*treatment + c_Pallisa*treatment + c_Mbarara*treatment")

# Implement SWE of ATE: Demean all covariates, fully interact
cols = c("sex","age","education_level","Arua","Ibanda","Pallisa","Shema",
         "village_distance_HF","village_distance_HOSP","village_distance_road","village_distance_electricity", "share2006")
datb[, paste0("c_", cols)] = lapply(datb[, cols], function(x) (x - mean(x))  )
covariatesb = c("treatment*c_village_distance_HF + treatment*c_village_distance_HOSP + treatment*c_village_distance_road + treatment*c_village_distance_electricity + 
                treatment*c_sex + c_age*treatment + c_education_level*treatment + treatment*c_share2006 + 
                c_Arua*treatment + c_Ibanda*treatment + c_Pallisa*treatment", 
                "c_Arua*treatment + c_Ibanda*treatment + c_Pallisa*treatment")


####################################################################################
## Responsiveness ####

# Unrestricted
for (i in names(dat[c("responsive_gov_index","responsive_dc_st","responsive_gak_st","responsive_gad_st",
                      "responsive_ngo_st","responsive_mp_st","responsive_lc_st")])){ # Dependent Variables
  for (j in covariates) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("m",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = dat))
    # Output clustered SEs (county)
    assign(x = paste("c",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = dat),
                            cluster.vcov(lm(as.formula(model), data = dat), dat$village_id)))
  }
}

# Restricted
for (i in names(datb[c("responsive_gov_index","responsive_dc_st","responsive_gak_st","responsive_gad_st",
                       "responsive_ngo_st","responsive_mp_st","responsive_lc_st")])){ # Dependent Variables
  for (j in covariatesb) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("bm",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = datb))
    # Output clustered SEs (county)
    assign(x = paste("bc",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = datb),
                            cluster.vcov(lm(as.formula(model), data = datb), datb$village_id)))
  }
}

####################################################################################
####################################################################################
## Table H.24

stargazer(m.responsive_gov_index.t, m.responsive_lc_st.t, m.responsive_dc_st.t, m.responsive_mp_st.t, m.responsive_gad_st.t, 
          m.responsive_gak_st.t, m.responsive_ngo_st.t,
          
          se = list(c.responsive_gov_index.t[,2], c.responsive_lc_st.t[,2], c.responsive_dc_st.t[,2], c.responsive_mp_st.t[,2], c.responsive_gad_st.t[,2], 
                    c.responsive_gak_st.t[,2], c.responsive_ngo_st.t[,2]),
          
          
          p = list(c.responsive_gov_index.t[,4], c.responsive_lc_st.t[,4], c.responsive_dc_st.t[,4], c.responsive_mp_st.t[,4], c.responsive_gad_st.t[,4], 
                   c.responsive_gak_st.t[,4], c.responsive_ngo_st.t[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/responsive_1_urc.tex",
          label = "tab:responsive_1_urc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Government and NGO Responsiveness",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Local", "District","MP", "District","National",
                            "NGO\\\\ & Index & Councilors & Chair & & Agency & Agency & "))

####################################################################################
####################################################################################
## Table M.61

stargazer(m.responsive_gov_index.c, m.responsive_lc_st.c, m.responsive_dc_st.c, m.responsive_mp_st.c, m.responsive_gad_st.c, 
          m.responsive_gak_st.c, m.responsive_ngo_st.c,
          
          se = list(c.responsive_gov_index.c[,2], c.responsive_lc_st.c[,2], c.responsive_dc_st.c[,2], c.responsive_mp_st.c[,2], c.responsive_gad_st.c[,2], 
                    c.responsive_gak_st.c[,2], c.responsive_ngo_st.c[,2]),
          
          
          p = list(c.responsive_gov_index.c[,4], c.responsive_lc_st.c[,4], c.responsive_dc_st.c[,4], c.responsive_mp_st.c[,4], c.responsive_gad_st.c[,4], 
                   c.responsive_gak_st.c[,4], c.responsive_ngo_st.c[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/responsive_1_urnc.tex",
          label = "tab:responsive_1_urnc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Government and NGO Responsiveness (No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Local", "District","MP", "District","National",
                            "NGO\\\\ & Index & Councilors & Chair & & Agency & Agency & "))


####################################################################################
####################################################################################
## Table H.25

stargazer(bm.responsive_gov_index.t, bm.responsive_lc_st.t, bm.responsive_dc_st.t, bm.responsive_mp_st.t, bm.responsive_gad_st.t, 
          bm.responsive_gak_st.t, bm.responsive_ngo_st.t,
          
          se = list(bc.responsive_gov_index.t[,2], bc.responsive_lc_st.t[,2], bc.responsive_dc_st.t[,2], bc.responsive_mp_st.t[,2], bc.responsive_gad_st.t[,2], 
                    bc.responsive_gak_st.t[,2], bc.responsive_ngo_st.t[,2]),
          
          
          p = list(bc.responsive_gov_index.t[,4], bc.responsive_lc_st.t[,4], bc.responsive_dc_st.t[,4], bc.responsive_mp_st.t[,4], bc.responsive_gad_st.t[,4], 
                   bc.responsive_gak_st.t[,4], bc.responsive_ngo_st.t[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/responsive_1_rc.tex",
          label = "tab:responsive_1_rc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Government and NGO Responsiveness (Restricted)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Local", "District","MP", "District","National",
                            "NGO\\\\ & Index & Councilors & Chair & & Agency & Agency & "))

####################################################################################
####################################################################################
## Table M.62

stargazer(bm.responsive_gov_index.c, bm.responsive_lc_st.c, bm.responsive_dc_st.c, bm.responsive_mp_st.c, bm.responsive_gad_st.c, 
          bm.responsive_gak_st.c, bm.responsive_ngo_st.c,
          
          se = list(bc.responsive_gov_index.c[,2], bc.responsive_lc_st.c[,2], bc.responsive_dc_st.c[,2], bc.responsive_mp_st.c[,2], bc.responsive_gad_st.c[,2], 
                    bc.responsive_gak_st.c[,2], bc.responsive_ngo_st.c[,2]),
          
          
          p = list(bc.responsive_gov_index.c[,4], bc.responsive_lc_st.c[,4], bc.responsive_dc_st.c[,4], bc.responsive_mp_st.c[,4], bc.responsive_gad_st.c[,4], 
                   bc.responsive_gak_st.c[,4], bc.responsive_ngo_st.c[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/responsive_1_rnc.tex",
          label = "tab:responsive_1_rnc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Government and NGO Responsiveness (Restricted; No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Local", "District","MP", "District","National",
                            "NGO\\\\ & Index & Councilors & Chair & & Agency & Agency & "))

rm(list =  grep("^(c|m)\\.", ls(), value = T))
rm(list = grep("^(bc|bm)\\.", ls(), value = T))


####################################################################################
# Efficacy

# Unrestricted
for (i in names(dat[c("efficacy_gov_index", "efficacy_contentious_index", "effect_gov_st","effect_ngo_st","effect_med_st",
                      "effect_demonstrate_st","effect_raise_comm_st")])){ # Dependent Variables
  for (j in covariates) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("m",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = dat))
    # Output clustered SEs (county)
    assign(x = paste("c",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = dat),
                            cluster.vcov(lm(as.formula(model), data = dat), dat$village_id)))
  }
}

# Restricted
for (i in names(datb[c("efficacy_gov_index", "efficacy_contentious_index", "effect_gov_st","effect_ngo_st","effect_med_st",
                       "effect_demonstrate_st","effect_raise_comm_st")])){ # Dependent Variables
  for (j in covariatesb) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("bm",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = datb))
    # Output clustered SEs (county)
    assign(x = paste("bc",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = datb),
                            cluster.vcov(lm(as.formula(model), data = datb), datb$village_id)))
  }
}

####################################################################################
####################################################################################
## Table H.26

stargazer(m.efficacy_gov_index.t, m.effect_gov_st.t, m.effect_raise_comm_st.t, m.effect_ngo_st.t, 
          m.efficacy_contentious_index.t,m.effect_med_st.t, m.effect_demonstrate_st.t, 
          
          se = list(c.efficacy_gov_index.t[,2], c.effect_gov_st.t[,2], c.effect_raise_comm_st.t[,2], c.effect_ngo_st.t[,2], 
                    c.efficacy_contentious_index.t[,2], c.effect_med_st.t[,2],c.effect_demonstrate_st.t[,2]),
          
          
          p = list(c.efficacy_gov_index.t[,4], c.effect_gov_st.t[,4], c.effect_raise_comm_st.t[,4], c.effect_ngo_st.t[,4], 
                   c.efficacy_contentious_index.t[,4], c.effect_med_st.t[,4],c.effect_demonstrate_st.t[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/responsive_2_urc.tex",
          label = "tab:responsive_2_urc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Efficacy of Political Engagement",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Contact", "Raise", "Contact", "Contentious", "Contact",
                            "Protest\\\\ & Index & Govt & Issue & NGO & Index & Media &"))

####################################################################################
####################################################################################
## Table M.63

stargazer(m.efficacy_gov_index.c, m.effect_gov_st.c, m.effect_raise_comm_st.c, m.effect_ngo_st.c, 
          m.efficacy_contentious_index.c,m.effect_med_st.c, m.effect_demonstrate_st.c, 
          
          se = list(c.efficacy_gov_index.c[,2], c.effect_gov_st.c[,2], c.effect_raise_comm_st.c[,2], c.effect_ngo_st.c[,2], 
                    c.efficacy_contentious_index.c[,2], c.effect_med_st.c[,2],c.effect_demonstrate_st.c[,2]),
          
          
          p = list(c.efficacy_gov_index.c[,4], c.effect_gov_st.c[,4], c.effect_raise_comm_st.c[,4], c.effect_ngo_st.c[,4], 
                   c.efficacy_contentious_index.c[,4], c.effect_med_st.c[,4],c.effect_demonstrate_st.c[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/responsive_2_urnc.tex",
          label = "tab:responsive_2_urnc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Efficacy of Political Engagement (No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Contact", "Raise", "Contact", "Contentious", "Contact",
                            "Protest\\\\ & Index & Govt & Issue & NGO & Index & Media &"))

####################################################################################
####################################################################################
## Table H.27

stargazer(bm.efficacy_gov_index.t, bm.effect_gov_st.t, bm.effect_raise_comm_st.t, bm.effect_ngo_st.t, 
          bm.efficacy_contentious_index.t,bm.effect_med_st.t, bm.effect_demonstrate_st.t, 
          
          se = list(bc.efficacy_gov_index.t[,2], bc.effect_gov_st.t[,2], bc.effect_raise_comm_st.t[,2], bc.effect_ngo_st.t[,2], 
                    bc.efficacy_contentious_index.t[,2], bc.effect_med_st.t[,2],bc.effect_demonstrate_st.t[,2]),
          
          p = list(bc.efficacy_gov_index.t[,4], bc.effect_gov_st.t[,4], bc.effect_raise_comm_st.t[,4], bc.effect_ngo_st.t[,4], 
                   bc.efficacy_contentious_index.t[,4], bc.effect_med_st.t[,4],bc.effect_demonstrate_st.t[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/responsive_2_rc.tex",
          label = "tab:responsive_2_rc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Efficacy of Political Engagement (Restricted)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Contact", "Raise", "Contact", "Contentious", "Contact",
                            "Protest\\\\ & Index & Govt & Issue & NGO & Index & Media &"))

####################################################################################
####################################################################################
## Table M.64

stargazer(bm.efficacy_gov_index.c, bm.effect_gov_st.c, bm.effect_raise_comm_st.c, bm.effect_ngo_st.c, 
          bm.efficacy_contentious_index.c,bm.effect_med_st.c, bm.effect_demonstrate_st.c, 
          
          se = list(bc.efficacy_gov_index.c[,2], bc.effect_gov_st.c[,2], bc.effect_raise_comm_st.c[,2], bc.effect_ngo_st.c[,2], 
                    bc.efficacy_contentious_index.c[,2], bc.effect_med_st.c[,2],bc.effect_demonstrate_st.c[,2]),
          
          p = list(bc.efficacy_gov_index.c[,4], bc.effect_gov_st.c[,4], bc.effect_raise_comm_st.c[,4], bc.effect_ngo_st.c[,4], 
                   bc.efficacy_contentious_index.c[,4], bc.effect_med_st.c[,4],bc.effect_demonstrate_st.c[,4]),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/responsive_2_rnc.tex",
          label = "tab:responsive_2_rnc", column.sep.width = "1pt", table.placement = "!ht", dep.var.caption = "",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Perceptions of Efficacy of Political Engagement (Restricted; No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Govt", "Contact", "Raise", "Contact", "Contentious", "Contact",
                            "Protest\\\\ & Index & Govt & Issue & NGO & Index & Media &"))

rm(list =  grep("^(c|m)\\.", ls(), value = T))
rm(list = grep("^(bc|bm)\\.", ls(), value = T))
